package com.lottery.im.module.setting.ui;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.lottery.im.MyApplication;
import com.lottery.im.R;
import com.lottery.im.api.Api;
import com.lottery.im.api.result.entity.UserInfo;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.constant.SpKeyConstant;
import com.lottery.im.module.main.ui.SplashActivity;
import com.lottery.im.module.setting.presenter.ProfilePresenter;
import com.lottery.im.module.setting.view.ProfileView;
import com.lottery.im.module.tim.business.LoginBusiness;
import com.lottery.im.util.AppManagerUtil;
import com.lottery.im.util.SPUtils;
import com.lottery.im.util.StatusBarHelper;
import com.lottery.im.util.ToastUtils;
import com.lottery.im.util.transformations.GlideCircleBorderTransform;
import com.tencent.imsdk.TIMCallBack;

/*
 *  个人资料
 * */
public class ProfileFragment extends BaseSwipeFragment<ProfilePresenter> implements ProfileView {

  // === 左进右出 ===
  //private TransitionConfig LEFT_TO_RIGHT = new TransitionConfig(
  //    R.anim.slide_in_left, R.anim.slide_out_right,
  //    R.anim.slide_in_right, R.anim.slide_out_left);

  @BindView(R.id.iv_portrait) ImageView mIvPortrait;
  @BindView(R.id.tv_name) TextView mTvName;
  @BindView(R.id.iv_vip) ImageView mIvVip;
  @BindView(R.id.tv_reg_time) TextView mTvRegTime;
  @BindView(R.id.tv_id) TextView mTvId;

  private final static int SETTING_REQUEST_CODE = 88;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_profile;
  }

  @Override protected void initPresenter() {
    presenter = new ProfilePresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {
    StatusBarHelper.setStatusBarLightMode(getActivity());
    setUI();
    presenter.getSelfLevel();
  }

  @OnClick({
      R.id.iv_back, R.id.tv_setting, R.id.iv_portrait, R.id.layout_reg_time, R.id.layout_id,
      R.id.layout_qr,
      R.id.layout_share,
      R.id.tv_exit
  }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        popBackStack();
        break;
      case R.id.tv_setting:
        startFragmentForResult(new SettingFragment(), SETTING_REQUEST_CODE);
        break;
      case R.id.iv_portrait:
        break;
      case R.id.layout_reg_time:
        break;
      case R.id.layout_id:
        break;
      case R.id.layout_qr:
        startFragment(new MyQrCodeFragment());
        break;
      case R.id.layout_share:
        startFragment(new ShareToFriendFragment());
        break;
      case R.id.tv_exit:
        LoginBusiness.logout(new TIMCallBack() {
          @Override public void onError(int i, String s) {
            ToastUtils.showToast(getActivity(), s);
          }

          @Override public void onSuccess() {
            SPUtils.put(getActivity(), SpKeyConstant.TOKEN_KEY, "");
            SPUtils.put(getActivity(), SpKeyConstant.USERINFO_KEY, "");
            AppManagerUtil.getAppManager().finishAllActivity();
            startActivity(new Intent(getActivity(), SplashActivity.class));
          }
        });
        break;
    }
  }

  private void setUI() {
    UserInfo userInfo = MyApplication.getInstance().getUserInfo();
    Glide.with(this)
        .load(Api.BASEADDRESS + userInfo.getUserPic())
        .apply(new RequestOptions()
            .bitmapTransform(new GlideCircleBorderTransform(
                mActivity.getResources().getDimension(R.dimen.portrait_border_width),
                ContextCompat.getColor(mActivity, R.color.white)))
            .placeholder(R.drawable.icon_conversation_de)
            .error(R.drawable.icon_conversation_de))
        .into(mIvPortrait);
    mTvName.setText(userInfo.getUserName());
    mTvId.setText(userInfo.getThirdAccId());
    String date = userInfo.getCreateDate();
    if (date.length() >= 8) {
      mTvRegTime.setText(
          date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8));
    }
  }

  @Override public void getSelfLevelSuc(int level) {
    if (level == 1) {
      mIvVip.setImageResource(R.drawable.ic_vip1);
    } else if (level == 2) {
      mIvVip.setImageResource(R.drawable.ic_vip2);
    } else if (level == 3) {
      mIvVip.setImageResource(R.drawable.ic_vip3);
    } else if (level == 4) {
      mIvVip.setImageResource(R.drawable.ic_vip4);
    } else {
      mIvVip.setImageResource(R.drawable.ic_vip5);
    }
  }

  @Override protected void onFragmentResult(int requestCode, int resultCode, Intent data) {
    super.onFragmentResult(requestCode, resultCode, data);

    if (requestCode == SETTING_REQUEST_CODE) {
      setUI();
    }
  }

  //@Override public TransitionConfig onFetchTransitionConfig() {
  //  return LEFT_TO_RIGHT;
  //}

  @Override public void onResume() {
    super.onResume();
    StatusBarHelper.setStatusBarLightMode(getActivity());
  }
}
